//
//  FJWelcomeViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/16.
//

import UIKit

class FJWelcomeViewController: FJBaseViewController {

    private let bgImageView         = c_make_ex(UIImageView.self)
        .image("FJ_SignIn_welcomeBg").mode(.scaleAspectFill).end()
    private let backView            = c_make_ex(UIView.self)
        .bgColor(.white).end()
    private let appleLogin          = UIIconButton(style: .leading(itemSpace: 10))
    private let loginButton         = c_make_ex(UIButton.self)
        .bgColor("#FF9133".toColor).corner(30).font(21.font_bold)
        .text("LOGIN").color(.white).end()
    private let setupButton         = c_make_ex(UIButton.self)
        .bgColor(.white).corner(30).font(21.font_bold).border("#FF9133".toColor, width: 3)
        .text("SIGN UP").color("#FF9133".toColor).end()
    private lazy var policyLabel        = ActiveLabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        view.backgroundColor = .blue
        view.subviews{
            bgImageView
            backView.subviews{
                appleLogin
                loginButton
                setupButton
                policyLabel
            }
        }
        backView.style { v in
            v.layer.cornerRadius = 20
            v.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        }
        bgImageView.fillContainer()
        backView.leading(0).trailing(0).bottom(0).height(340)
        appleLogin.top(35).leading(20).trailing(20).height(60)
        loginButton.leading(20).trailing(20).height(60)
        loginButton.Top == appleLogin.Bottom + 15
        setupButton.leading(20).trailing(20).height(60)
        setupButton.Top == loginButton.Bottom + 15
        policyLabel.centerHorizontally().bottom(45)
//        policyLabel.Top == setupButton.Bottom + 30
        
        appleLogin.style { v in
            v.setImage("FJ_SignIn_Apple".toImage, for: .normal)
            v.iconSize = CGSize(width: 21, height: 25)
            v.setTitle("Sign up with Apple", for: .normal)
            v.setTitleColor(.white, for: .normal)
            v.titleLabel?.font = 21.font_bold
            v.backgroundColor = .black
            v.layer.cornerRadius = 30
            v.layer.masksToBounds = true
        }
        
        appleLogin.addTarget(self, action: #selector(clickAppleButton), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        setupButton.addTarget(self, action: #selector(clickSetupButton), for: .touchUpInside)
    }

    private func configTermsAndPolicyStyle() {
        let customType1 = ActiveType.custom(pattern: "Terms of Use")
        let customType2 = ActiveType.custom(pattern: "Privacy Policy")
        policyLabel.isShowUnderLine = true
        policyLabel.font = 16.font_bold
        policyLabel.numberOfLines = 0
        policyLabel.textAlignment = .center
        policyLabel.enabledTypes = [customType1, customType2]
        policyLabel.text = "Terms of Use & Privacy Policy"
        policyLabel.textColor = "#FF9133".toColor
        policyLabel.customColor[customType1] = .black
        policyLabel.customColor[customType2] = .black
        
        policyLabel.handleCustomTap(for: customType1) { [weak self] str in
            
        }
        policyLabel.handleCustomTap(for: customType2) { [weak self] str in
            
        }
    }
}

//MARK: action
extension FJWelcomeViewController {
    
    @objc private func clickLoginButton(){
        
        self.navigationController?.pushViewController(FJLoginViewController(), animated: true)
    }
    
    @objc private func clickSetupButton(){
        
        self.navigationController?.pushViewController(FJSetupViewController(), animated: true)
    }
    
    @objc private func clickAppleButton(){
        
        AppleLoginTool.instence.show { user, token in
            RT.appleLogininWith(emailNum: user, Password: "") { info in
                ToastTool.dismiss()
                if let sym = info.info9, sym.count > 0 {
                    let str = GetInfo(key: "UserInfo") as! NSDictionary
                    let jsonData = InfoUtil.initInfo.deserialize(from: str)
                    USEINFOMODEL = jsonData!
                    UIApplication.window?.rootViewController = FJNavViewController(rootViewController:  FJHomeViewController())
                } else {
                    //新用户
                    let vc = FJSetUpInfoViewController()
                    UIApplication.topController()?.navigationController?.pushViewController(vc, animated: true)
                }

            }
        } failure: { error in
            error.errorDescripton.toast_error
        }
    }
}
